package com.imoblife.brainwavestus.share;


import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.listener.FbAPIsAuthListener;
import com.facebook.listener.FbAPIsLogoutListener;
import com.imoblife.brainwavestus.R;

public class ShareUtil {

	private Activity mActivity;
	private Context mContext;
	String[] item;
	String[] permissions = { "offline_access", "publish_stream","email"};
	//Facebook 
	public static final String APP_ID = "357997364255070"; 
	LoginUtil loginUtil;
	

	public ShareUtil(Activity activity) {
		this.mActivity = activity;
		this.mContext = activity;
		String[] temp = { mActivity.getString(R.string.facebook_title),
				mActivity.getString(R.string.twitter_title),
				mActivity.getString(R.string.email_title),
				mActivity.getString(R.string.sms_title) };
		item = temp;
	}

	public void ShareItem(String which, String url, String title, String content) {

		if (which.equals("twitter")) {
			this.shareTwitter(url, title, content);
		}

		if (which.equals("facebook")) {
			this.shareFacebook(url, title, content);
		}

		if (which.equals("email")) {
			this.sendEmail(url, title, content);
		}

		if (which.equals("sms")) {
			this.sendSMS(url, content);
		}

	}

	private void shareFacebook(String url, String title, String content) {
		if (Locale.getDefault().toString().contains("zh")) {
			Toast.makeText(mActivity, "抱歉，您所在的地区不支持此服务。", Toast.LENGTH_LONG)
					.show();
		} else {
			Utility.mFacebook = new Facebook(APP_ID);
			Utility.mAsyncRunner = new AsyncFacebookRunner(Utility.mFacebook);
			SessionStore.restore(Utility.mFacebook, mActivity);
			SessionEvents.addAuthListener(new FbAPIsAuthListener(mActivity));
			SessionEvents.addLogoutListener(new FbAPIsLogoutListener(mActivity));
			loginUtil = new LoginUtil();
			loginUtil.init(mActivity, -1, Utility.mFacebook, permissions,url,title,content);
			loginUtil.loginAuthorize();
		}
	}

	private void shareTwitter(String url, String title, String content) {
		if (Locale.getDefault().toString().contains("zh")) {
			Toast.makeText(mActivity, "抱歉，您所在的地区不支持此服务。", Toast.LENGTH_LONG)
					.show();
		} else {
			Intent intent = new Intent(mContext, TwitterShareActivity.class);
			intent.putExtra("share_url", url);
			intent.putExtra("share_title", title);
			intent.putExtra("share_content", "");
			intent.putExtra("share_extr", "(via @Brainwaves - T.U.S®)");
			mContext.startActivity(intent);
		}

	}

	private void sendSMS(String url, String content) {
		Uri uri = Uri.parse("smsto:" + "");
		Intent it = new Intent();
		it.setAction(Intent.ACTION_SENDTO);
		it.putExtra("sms_body", content + " " + url);
		it.setType("vnd.android-dir/mms-sms");
		it.setData(uri);
		mActivity.startActivity(it);
	}

	private void sendEmail(String url, String title, String content) {
		Intent emailIntent = new Intent(Intent.ACTION_SEND);
		emailIntent.setType("plain/text");
		emailIntent.putExtra(Intent.EXTRA_EMAIL, "");
		emailIntent.putExtra(Intent.EXTRA_SUBJECT, title);
		emailIntent.putExtra(Intent.EXTRA_TEXT, content + " " + url);
		mActivity.startActivity(emailIntent);

	}

	public void showShareDialog(String dialogTitle, final String share_url,
			final String share_title, final String share_content) {
		new AlertDialog.Builder(mActivity).setTitle(dialogTitle)
				.setItems(item, new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:
							ShareItem("facebook", share_url, share_title,
									share_content);
							break;
						case 1:
							ShareItem("twitter", share_url, share_title,
									share_content);
							break;
						case 2:
							ShareItem("email", share_url, share_title,
									share_content);
							break;
						case 3:
							ShareItem("sms", share_url, share_title,
									share_content);
							break;
						default:
							break;
						}
					}

				}).create().show();
	}

}
